<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="100" align="center"></el-table-column>
      <el-table-column prop="pinyin" label="拼音"width="100" align="center"></el-table-column>
      <el-table-column prop="logo" label="logo" width="100" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" width="100" align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键字" width="100" align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
      <el-table-column prop="sales" label="销量" width="100" align="center"></el-table-column>
      <el-table-column prop="productCount" label="商品种类数量总和" width="100" align="center"></el-table-column>
      <el-table-column prop="commentCount" label="买家评论数量总和" width="100" align="center"></el-table-column>
      <el-table-column prop="positiveCommentCount" label="买家好评数量总和" width="100" align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="100" align="center">
        <el-table-column label="是否启用" width="80" align="center">
          <template slot-scope="scope">
            <el-switch
                @change="changeEnable(scope.row)"
                v-model="scope.row.enable"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit"
                     circle size="mini" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete"
                     circle size="mini" @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods:{
    changeEnable(brand){
      let enableText = ['禁用','启用'];
      let url = 'http://localhost:9080/brands/'+brand.id;
      if(brand.enable==1){
        console.log('将启用该品牌');
        url += '/enable';
      }else{
        console.log('将禁用该品牌');
        url +='/disable';
      }
      this.axios.post(url).then((response)=>{
        let responseBody= response.data;
        if(responseBody.state==20000){
          let message = '将该品牌'+brand.name+'的状态设置为'+enableText[brand.enable];
          this.$message({
            message:message,
            type: 'success'
          });
        }else {
          this.$message.error(responseBody.message);
        }
        if(responseBody.state==40400){
          this.loadAdminList();
        }
      });
    },
    handleEdit(brand){
      let message = '您正在尝试编辑【' + brand.id + '-' + brand.name + '】的品牌详情，抱歉，该功能尚未实现……';
      this.$alert(message, '提示', {
        confirmButtonText: '确定'
      });
    },
    handleDelete(brand){
      console.log("处理删除..."+brand.name)
      let url = 'http://localhost:9080/brands/'+brand.id+'/delete';
      console.log('url ='+url)
      this.axios.post(url).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          this.loadBrandList();
        }else {
          this.$message.error(responseBody.message);
          this.loadBrandList();
        }
      })
    },
    openDeleteConfirm(brand){
      let message = '此操作将永久删除【'+brand.name+'】品牌,是否继续';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '那你点我干嘛 傻杯'
        });
      });
    },
    loadBrandList(){
      console.log("loadBrandListList");
      let url="http://localhost:9080/brands"
      console.log("url="+url);
      this.axios.get(url).then((response)=>{
        let responseBody = response.data;
        this.tableData = responseBody.data;
      })
    }
  },
  mounted() {
    console.log("mounted");
    this.loadBrandList();
  }
}
</script>
